<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="app">
    <home-page></home-page>
  </div>
  <script src="./vue.js"></script>
  <script>
    // 外部定义第三方实例
    const bus = new Vue();
    const CommonHead = {
      template: `
        <div>
          <h3>这是子组件1</h3>  
          <button @click="emit">向父组件发送数据</button>
        </div>
      `,
      data(){
        return {
          msg: '这是子组件1数据'
        }
      },
      methods: {
        emit(){
          bus.$emit('biubiu', this.msg);
        }
      }
    }
    const CommonHead2 = {
      template: `
        <div>
          <h3>这是子组件2</h3>  
          {{ msg }}
        </div>
      `,
      data(){
        return {
          msg: ''
        }
      },
      mounted(){
        bus.$on('biubiu', (msg) => {
          this.msg = msg;
        })
       /*  bus.$on('biubiu', function(msg){
          this.msg = msg;
        }.bind(this)) */
      }
    }
    // 页面
    const Home = {
      template: `
        <div>
          这是home页  
         
          <hr/>
          <common-head></common-head>
          <hr/>
          <common-head2></common-head2>
        </div>
      `,
      components: {
        CommonHead,
        CommonHead2
      }
    }

    Vue.component('HomePage', Home)
    const vm = new Vue({
      el: '#app'
    })
  </script>
</body>
</html>